package com.lzx.ai.controller;

import com.lzx.ai.entity.Article;
import com.lzx.ai.utils.KeywordExtractor;
import com.lzx.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 程序员星星
 * @date 2023/4/14
 * @Description
 */
@RestController
@RequestMapping("/ai/textAnalysis/web")
public class TextAnalysisController {
//    private JiebaKeywordExtractor keywordExtractor;
//
//    public void ArticleController(JiebaKeywordExtractor keywordExtractor) {
//        this.keywordExtractor = keywordExtractor;
//    }
//
//    public TextAnalysisController() {
//        keywordExtractor = null;
//    }

    //文章内容提取
//    @PostMapping("/text")
//    public R getArticle(@RequestBody Article article) {
//        //提取关键词
//        List<String> list = JiebaKeywordExtractor.extractKeywords(article.getContent());
//        //列表长度
//        int length = list.size();
//        //随机提取3个关键词
//        if (list.size() > 3) {
//            //返回给前端的关键词列表
//            List<String> keys = new ArrayList<>();
//            HashSet<Integer> set = new HashSet<>();
//            while (true) {
//                if (set.add((int) (Math.random() * length))) {
//                    keys.add(list.get((int) (Math.random() * length)));
//                    set.add((int) (Math.random() * length));
//                    if (keys.size() == 3) {
//                        break;
//                    }
//                }
//            }
//            return R.ok().data("list", keys);
//        } else {
//            return R.ok().data("list", list);
//        }
//    }

    //文章内容提取
    @PostMapping("/text")
    public R extractText(@RequestBody Article article) {
        List<String> list = new KeywordExtractor().extract(article.getContent());
        return R.ok().data("list", list);
    }
}
